<?php

namespace App\Admin\Models;

use App\Admin\Models\BaseModel;

use Encore\Admin\Facades\Admin;

use Ixudra\Curl\Facades\Curl;

use DB;

class Common extends BaseModel
{
	protected $table = 'temp';
    
    //指定主键
    protected $primaryKey = 'id';
    
    // 获取分销规则列表
    public function getPreceptSelect(){
    	
    	$sId = Admin::user()->sId;
    	$url = $this->host."/distributionPrecept/getList?dcKey={$sId}&page=1&perPage=1000";
    	$response = Curl::to($url)->get();
    	$data = $this->handelApiData($response);

    	$selectArray = array();
    	foreach ($data['data'] as $pInfo){
    		$selectArray[$pInfo['dpId']] = $pInfo['dpName'];
    	}
    	
    	return $selectArray;
    }
    
    // 获取分销规则组
    public function getPreceptGroupSelect(){
    	
    	$sId = Admin::user()->sId;
    	$url = $this->host."/distributionGroup/getList?dcKey={$sId}&page=1&perPage=1000";
    	$response = Curl::to($url)->get();
    	$data = $this->handelApiData($response);
    	
    	$selectArray = array();
    	foreach ($data['data'] as $pInfo){
    		$selectArray[$pInfo['dlgId']] = $pInfo['dlgName'];
    	}
    	
    	return $selectArray;
    }
    
    // 根据id获取用户身份等级
    public function getDistributionLevel($userId){
    	
    	$sId = Admin::user()->sId;
		$host = config('admin.dataApiHost');
		$url = $host."/member/getUserInfoByUserId?mcKey={$sId}&userId={$userId}";

		$data = Curl::to($url)->get();

		$data = json_decode($data, true);
		$info = $data['data'];
		if (isset($info['alId'])){
			return $info['alId'];
		}
		
		$info = DB::table('admin_users')->where('sId', $sId)->where('auCode', $userId)->first();
		if (isset($info['alId'])){
			return $info['alId'];
		}
		
		return 1;
    }
    
    // 根据id获取用户身份等级
    public function getStroeInfo(){
    	
    	$sId = Admin::user()->sId;
		$host = config('admin.dataApiHost');
		$url = $host."/store/get/{$sId}";

		$data = Curl::to($url)->get();
		$data = json_decode($data, true);
		$info = $data['data'];

		return $info;
    }
    
    // 根据id获取用户身份等级信息
    public function getLevelInfo($alId){
    	
    	$sId = Admin::user()->sId;
		$host = config('admin.dataApiHost');
		$url = $host."/agencyLevel/get/{$alId}?acKey={$sId}";

		$data = Curl::to($url)->get();
		$data = json_decode($data, true);
		$info = $data['data'];

		return $info;
    }
    
    // 根据id获取用户身份等级
    public function getLevelId($alId){
    	
    	$info = $this->getLevelId($alId);

		return $info['alLevel'];
    }
    
}
